package com.hhd.form.table;

import com.hhd.base.utils.CommonUtils;
import com.hhd.form.entity.DesignFormDefinition;
import com.hhd.form.entity.DesignFormTableMeta;
import com.hhd.form.service.IDesignFormTableMetaService;
import com.hhd.form.table.db.DBOperator;
import com.hhd.form.vo.FormDefinition;
import com.hhd.form.vo.SubFormDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class FormTableBuilder {
    @Autowired
    IDesignFormTableMetaService tableMetaService;
    @Autowired
    DBOperator dbOperator;
    private static final String formmainTable = "table_formmain_";

    private static final String formsonTable = "table_formson_";

    public void buildFormTables(FormDefinition formDefinition) {
        resolveFormMainFields(formDefinition);
        if (CommonUtils.isEmpty(formDefinition.getTableName()) || notExistTable(formDefinition.getTableName())) {
            //表不存在, 需要建表
            DesignFormTableMeta tableMeta = buildFormmainTable(formDefinition);
            doCreateTable(tableMeta, formDefinition);
        } else {
            updateTableComment(formDefinition, tableMetaService.lambdaQuery().eq(DesignFormTableMeta::getTableName, formDefinition.getTableName()).list().get(0));
        }
        if (CommonUtils.isEmpty(formDefinition.getSubForms())) return;
        buildFormsonTables(formDefinition);
    }

    private boolean notExistTable(String tableName) {
        if (CommonUtils.isEmpty(tableName)) return true;
        return tableMetaService.lambdaQuery().eq(DesignFormTableMeta::getTableName, tableName).count() == 0;
    }

    private static void resolveFormMainFields(FormDefinition formDefinition) {
        formDefinition.setFormMain(true);
        formDefinition.setDel(false);
        formDefinition.setUsed(false);
        if (CommonUtils.isEmpty(formDefinition.getSubTableIndex())) {
            formDefinition.setSubTableIndex(0);
        }
    }

    private void buildFormsonTables(FormDefinition fomDefinition) {
        int index = 0;
        List<DesignFormTableMeta> list = tableMetaService.lambdaQuery()
                .eq(DesignFormTableMeta::getFormMain, false)
                .orderByDesc(DesignFormTableMeta::getTableIndex)
                .list();
        if (!CommonUtils.isEmpty(list)) index = list.get(0).getTableIndex();

        for (SubFormDefinition subForm : fomDefinition.getSubForms()) {
            resolveSubFormFields(fomDefinition, subForm);
            if (CommonUtils.isEmpty(subForm.getTableName()) || notExistTable(subForm.getTableName())) {
                //表不存在, 需要建表
                DesignFormTableMeta tableMeta = createTableMeta(++index, subForm);
                doCreateTable(tableMeta, subForm);
            } else {
                updateTableComment(subForm, tableMetaService.lambdaQuery().eq(DesignFormTableMeta::getTableName, subForm.getTableName()).list().get(0));
            }
        }
    }

    private static void resolveSubFormFields(FormDefinition fomDefinition, SubFormDefinition subForm) {
        subForm.setFormMain(false);
        subForm.setAppId(fomDefinition.getAppId());
        subForm.setDel(false);
        subForm.setHasFlow(false);
    }

    private void doCreateTable(DesignFormTableMeta tableMeta, DesignFormDefinition formDefinition) {
        dbOperator.doCreateTable(tableMeta, formDefinition);
    }

    private void updateTableComment(FormDefinition formDefinition, DesignFormTableMeta designFormTableMeta) {
        dbOperator.updateTableComment(formDefinition, designFormTableMeta);
    }

    private DesignFormTableMeta buildFormmainTable(FormDefinition fomDefinition) {
        int index = 0;
        List<DesignFormTableMeta> list = tableMetaService.lambdaQuery()
                .eq(DesignFormTableMeta::getFormMain, true)
                .orderByDesc(DesignFormTableMeta::getTableIndex)
                .list();
        if (!CommonUtils.isEmpty(list)) index = list.get(0).getTableIndex();
        return createTableMeta(++index, fomDefinition);
    }

    private DesignFormTableMeta createTableMeta(int index, DesignFormDefinition formDefinition) {
        DesignFormTableMeta tableMeta = new DesignFormTableMeta();

        if (!CommonUtils.isEmpty(formDefinition.getTableName())) {
            tableMeta.setTableName(formDefinition.getTableName());
            tableMeta.setTableIndex(-1);
        } else {
            tableMeta.setTableIndex(index);
            if (formDefinition.getFormMain()) {
                tableMeta.setTableName(formmainTable + createFieldIndexStr(index));
            } else {
                tableMeta.setTableName(formsonTable + createFieldIndexStr(index));
            }
        }
        tableMeta.setFormMain(formDefinition.getFormMain());
        tableMeta.setDel(false);
        tableMeta.setFormId(formDefinition.getId());
        tableMeta.setComment(formDefinition.getFormName());
        tableMetaService.save(tableMeta);
        formDefinition.setTableName(tableMeta.getTableName());
        tableMetaService.saveOrUpdate(tableMeta);
        return tableMeta;
    }

    private String createFieldIndexStr(int index) {
        StringBuilder res = new StringBuilder(index + "");
        while (res.length() < 5) res.insert(0, "0");
        return res.toString();
    }
}
